/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.recursos.filter;

/**
 *
 * @author VYG0014
 */
import com.recursos.controlador.ControladorLogin;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FilterLogin implements Filter {

    private FilterConfig config;
    private String urlLogin;

    public void init(FilterConfig config) throws ServletException {
        urlLogin = config.getInitParameter("urlLogin");
        this.config = config;
    }

    public void destroy() {
        config = null;
    }

     public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
				
		HttpServletRequest request = ((HttpServletRequest) arg0);
		HttpSession session = request.getSession();
		if(session.getAttribute("user")==null){
			RequestDispatcher noPermited = request.getRequestDispatcher("/"+urlLogin);
			noPermited.forward(arg0, arg1);
		}else{
			arg2.doFilter(arg0, arg1);
		}
		
	}
}
